package com.qc31.monitorlib.ui.other

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import autodispose2.autoDispose
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.DraggableModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.qc31.baselib.base.BaseRecycleActivity
import com.qc31.baselib.utils.GsonUtil
import com.qc31.baselib.utils.ToolbarHelper
import com.qc31.comlib.Keys
import com.qc31.comlib.UserConstant
import com.qc31.monitorlib.login.LocationStatusModel
import com.qc31.comlib.map.AppGpsColumn
import com.qc31.monitorlib.R
import com.qc31.monitorlib.databinding.ActivityToolbarListBinding
import com.qc31.monitorlib.uitls.AppConfigUtil

/** @Author pc_hfjs @Date 2022/1/13 17:20 @Desc */
class InfoSetActivity :
    BaseRecycleActivity<ActivityToolbarListBinding>(ActivityToolbarListBinding::inflate) {
    private lateinit var toolbarView: TextView
    private val mAdapter by lazy { InfoSetAdapter() }
    private val editAction by lazy { getString(R.string.desc_com_edit) }
    private val sureAction by lazy { getString(R.string.desc_com_sure) }

    override fun initView() {
        super.initView()
        toolbarView = ToolbarHelper.setToolbarView(
            this,
            getString(R.string.desc_location_info_set),
            getString(R.string.desc_com_edit)
        )
        mRecyclerVB.recyclerView.layoutManager = LinearLayoutManager(this)
        mRecyclerVB.recyclerView.adapter = mAdapter
        mAdapter.setList(LocationStatusModel.changeList)

        AppConfigUtil.toastLive.autoDispose(scopeProvider).subscribe(this::toastObserver)
        AppConfigUtil.saveLive.autoDispose(scopeProvider).subscribe {
            finish()
        }
    }

    override fun setListener() {
        toolbarView.setOnClickListener {
            if (toolbarView.text == editAction) {
                toolbarView.text = sureAction
                mAdapter.isEdit = true
                mAdapter.draggableModule.isDragEnabled = true
                mAdapter.notifyDataSetChanged()
            } else {
                LocationStatusModel.update(mAdapter.data)
                LocationStatusModel.saveLocal()
                if (UserConstant.loginType == Keys.LOGIN_TYPE_CAR) {
                    AppConfigUtil.saveCarConfig(
                        UserConstant.current_carId ?: "",
                        GsonUtil.toJson(LocationStatusModel.changeList)
                    )
                } else {
                    AppConfigUtil.saveConfig(
                        3,
                        GsonUtil.toJson(LocationStatusModel.changeList)
                    )
                }

                finish()
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        LocationStatusModel.update(mAdapter.data)
        LocationStatusModel.saveLocal()
        if (UserConstant.loginType == Keys.LOGIN_TYPE_CAR) {
            AppConfigUtil.saveCarConfig(
                UserConstant.current_carId ?: "",
                GsonUtil.toJson(LocationStatusModel.changeList)
            )
        } else {
            AppConfigUtil.saveConfig(
                3,
                GsonUtil.toJson(LocationStatusModel.changeList)
            )
        }
    }

    class InfoSetAdapter :
        BaseQuickAdapter<AppGpsColumn, BaseViewHolder>(R.layout.item_info_set),
        DraggableModule {
        var isEdit = false
        override fun convert(holder: BaseViewHolder, item: AppGpsColumn) {
            holder.setText(
                R.id.tvSetTitle, context.getString(LocationStatusModel.getDescByKey(item.key))
            )
            val switch = holder.getView<ImageView>(R.id.ivSetSwitch)
            val move = holder.getView<ImageView>(R.id.ivSetMove)
            if (isEdit) {
                switch.visibility = View.GONE
                move.visibility = View.VISIBLE

            } else {
                switch.isSelected = item.switch == 1
                switch.setOnClickListener {
                    switch.isSelected = !switch.isSelected
                    item.switch = if (switch.isSelected) 1 else 0
                }
            }

        }
    }
}